from QMemberType import QMemberType
# event
from EventDefine import event_QMEMBERLISTMGR_MEMBERJOIN, \
                        event_QMEMBERLISTMGR_MEMBERMOVE,\
                        event_QMEMBERLISTMGR_MEMBERLEAVE

class QMemberListMgr:
    def __init__(self, memberList):
        self.MemberList = memberList
        self.MyMemberID = None
        self.ID2MemberMap = self.ID2MemberMapGet()

    def __repr__(self):
        ret = "\n".join([item.__repr__() for item in self.MemberList])
        return ret

    def MemberListGet(self):
        ret = self.MemberList
        return ret

    def MyMemberIDSet(self, memberID):
        self.MyMemberID = memberID

    def MyMemberIDGet(self):
        ret = self.MyMemberID
        return ret

    def ID2MemberMapGet(self):
        id2MemberMap = {}
        for member in self.MemberList:
            id2MemberMap[member.MemberID] = member
        return id2MemberMap

    def ID2MemberGet(self, memberID):
        member = self.ID2MemberMap[memberID]
        return member

    def MemberTypeCount(self, memType):
        ret = sum([1 for member in self.MemberList
                     if memType in member.OptionMemberType])
        return ret

    def ValidHumanMemberTypeCount(self):
        ret = self.MemberTypeCount(memType=QMemberType.HUMAN)
        return ret

    def FirstValidMemberIDGet(self):
        for member in self.MemberList:
            if member.CurrentMemberType == QMemberType.OPEN:
                ret = member.MemberID
                break
        else:
            ret = None
        return ret

    def MemberJoin(self,
                   memName,
                   memType=QMemberType.HUMAN,
                   memID=None):
        if memID is None:
            memID = self.FirstValidMemberIDGet()
            if memID is None:
                raise 0, "Have no valid slot; Member can not join."

        member = self.ID2MemberMap[memID]
        bSuccess = member.MemberSet(memName, memType)

        # raise event
        messenger.send(event_QMEMBERLISTMGR_MEMBERJOIN, [memName, memType, memID])
        return memID

    def MemberLeave(self, memberID):
        member = self.ID2MemberMap[memberID]
        memberName = member.MemberName
        member.MemberClear()
        # raise event
        messenger.send(event_QMEMBERLISTMGR_MEMBERLEAVE, [memberID, memberName])

    def MemberMove(self, memberID, targetTeamID):
        member = self.ID2MemberMap[memberID]
        bTheSameTeamID = (member.TeamID == targetTeamID)
        tmpFirstMember = None
        for tmpMember in self.MemberList:
            if (tmpMember.TeamID == targetTeamID) and \
               (tmpMember.CurrentMemberType == QMemberType.OPEN) and \
               (QMemberType.HUMAN in tmpMember.OptionMemberType):
                if bTheSameTeamID and (tmpFirstMember is None):
                    tmpFirstMember = tmpMember

                if (bTheSameTeamID and (tmpMember.MemberID > member.MemberID)) or \
                   (not bTheSameTeamID):
                    newMemberID = self._DoMemberMove(member, tmpMember)
                    break
        else:
            if bTheSameTeamID and (tmpFirstMember is not None):
                newMemberID = self._DoMemberMove(member, tmpFirstMember)
            else:
                newMemberID = None

        # raise event latter
        return newMemberID

    def _DoMemberMove(self, fromMember, toMember):
        toMember.MemberSet(fromMember.MemberName, fromMember.CurrentMemberType)
        fromMember.MemberClear()
        newMemberID = toMember.MemberID
        return newMemberID

    def MemberMoveEventRaise(self, oldMemberID, newMemberID):
        messenger.send(event_QMEMBERLISTMGR_MEMBERMOVE, [oldMemberID, newMemberID])
